I've just been having a little mess about and thought that I'd try and make a little program that takes input from the user and puts it into an array. This lead me to this information about fgets() and this post that has some code example on it.
But I used this to make a little program that works, and I was just wondering about how I could refine it and make the 'design' aspect of it better, I haven't made any functions yet.
here's the code though :
heres a link if you'd rather look at it on pastebin with syntax highlighting!
So I'm thinking about using functions in the program, but I'm not too sure where I'd do that in this example (maybe it's too small to do practically?)Code:/* Name & Phone number program Needs a total of 2 arrays, one for the name and one for the phone number. */ #include <stdio.h> #include <string.h> int main(void) { int i; char names[5][20]; char phoneNumber[5][11]; int select; printf("Please enter names : \n\n"); for ( i = 0 ; i < 5 ; i++ ) { fgets( names[i], sizeof names[i], stdin ); names[i][strlen(names[i]) - 1] = '\0'; //just subtract 1 to overwrite the '\n' with null terminator } puts("Please enter phone numbers : "); for ( i = 0 ; i < 5 ; i++ ) { fgets( phoneNumber[i], sizeof phoneNumber[i], stdin ); phoneNumber[i][strlen(phoneNumber[i]) - 1] = '\0'; //just subtract 1 to overwrite the '\n' with null terminator } puts("Which array would you like? "); scanf("%i", &select); printf("name %i is : %s\n", select, names[select]); printf("number %i is : %s\n", select, phoneNumber[select]); printf("\n"); return 0; }
The two for loops are fairly dependant on the actual code around them, and I was thinking that perhaps the functions should be free? I'm not sure why I think that.
So it's the design element that I'm trying to focus on with it at the moment, although any other advice would be much appreciated.